package fr.labri.buganalyzer.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class PrepareInputXMLfiles {


	public static void main(String[] args) throws IOException {
		String inputfolder = args[0];
		File directory = new File(inputfolder);

		if(directory.exists() && directory.isDirectory()){
			File[] files = directory.listFiles();
			for(File fi : files){
				if (fi.getName().endsWith(".xml")){
					File fo = new File(directory.getAbsoluteFile() +"/"+ fi.getName() +"____.xml");
					replace(fi,fo);
					//fi.delete();
					System.out.println("file "+ fi.getName()+" processed...");
				}
			}
		}
	}

	private static void replace(File in, File out)
			throws IOException {

		BufferedReader reader = new BufferedReader(new FileReader(in));

		PrintWriter writer = new PrintWriter(new FileWriter(out));
		String line = null;
		while ((line = reader.readLine()) != null)
			writer.println(line.replaceAll("&Agrave;","&#192;")
					
					.replaceAll("&curren;","&#164;")
					.replaceAll("&yen;","&#165;")
					.replaceAll("&sect;","&#167;")
					.replaceAll("&uml;","&#168;")
					.replaceAll("&copy;","&#169;")
					.replaceAll("&laquo;","&#171;")
					.replaceAll("&not;","&#172;")
					.replaceAll("&reg;","&#174;")
					.replaceAll("&para;","&#182;")
					.replaceAll("&raquo;","&#187;")
					.replaceAll("&frac12;","&#189;")
					.replaceAll("&iquest;","&#191;")
					.replaceAll("&Agrave;","&#192;")
					
					
					
					.replaceAll("&Aacute;","&#193;")
					.replaceAll("&Acirc;","&#194;")
					.replaceAll("&Atilde;","&#195;")
					.replaceAll("&Auml;","&#196;")
					.replaceAll("&Aring;","&#197;")
					.replaceAll("&AElig;","&#198;")
					.replaceAll("&Ccedil;","&#199;")
					.replaceAll("&Egrave;","&#200;")
					.replaceAll("&Eacute;","&#201;")
					.replaceAll("&Ecirc;","&#202;")
					.replaceAll("&Euml;","&#203;")
					.replaceAll("&Igrave;","&#204;")
					.replaceAll("&Iacute;","&#205;")
					.replaceAll("&Icirc;","&#206;")
					.replaceAll("&Iuml;","&#207;")
					.replaceAll("&ETH;","&#208;")
					.replaceAll("&Ntilde;","&#209;")
					.replaceAll("&Ograve;","&#210;")
					.replaceAll("&Oacute;","&#211;")
					.replaceAll("&Ocirc;","&#212;")
					.replaceAll("&Otilde;","&#213;")
					.replaceAll("&Ouml;","&#214;")
					.replaceAll("&Oslash;","&#216;")
					.replaceAll("&Ugrave;","&#217;")
					.replaceAll("&Uacute;","&#218;")
					.replaceAll("&Ucirc;","&#219;")
					.replaceAll("&Uuml;","&#220;")
					.replaceAll("&Yacute;","&#221;")
					.replaceAll("&THORN;","&#222;")
					.replaceAll("&szlig;","&#223;")
					.replaceAll("&agrave;","&#224;")
					.replaceAll("&aacute;","&#225;")
					.replaceAll("&acirc;","&#226;")
					.replaceAll("&atilde;","&#227;")
					.replaceAll("&auml;","&#228;")
					.replaceAll("&aring;","&#229;")
					.replaceAll("&aelig;","&#230;")
					.replaceAll("&ccedil;","&#231;")
					.replaceAll("&egrave;","&#232;")
					.replaceAll("&eacute;","&#233;")
					.replaceAll("&ecirc;","&#234;")
					.replaceAll("&euml;","&#235;")
					.replaceAll("&igrave;","&#236;")
					.replaceAll("&iacute;","&#237;")
					.replaceAll("&icirc;","&#238;")
					.replaceAll("&iuml;","&#239;")
					.replaceAll("&eth;","&#240;")
					.replaceAll("&ntilde;","&#241;")
					.replaceAll("&ograve;","&#242;")
					.replaceAll("&oacute;","&#243;")
					.replaceAll("&ocirc;","&#244;")
					.replaceAll("&otilde;","&#245;")
					.replaceAll("&ouml;","&#246;")
					.replaceAll("&oslash;","&#248;")
					.replaceAll("&ugrave;","&#249;")
					.replaceAll("&uacute;","&#250;")
					.replaceAll("&ucirc;","&#251;")
					.replaceAll("&uuml;","&#252;")
					.replaceAll("&yacute;","&#253;")
					.replaceAll("&thorn;","&#254;")
					.replaceAll("&yuml;","&#255;")
					);	

		// I'm aware of the potential for resource leaks here. Proper resource
		// handling has been omitted in the interest of brevity
		reader.close();
		writer.close();
		
	}

}
